//
//  HttpRequest.m
//  Block
//
//  Created by MAC on 15/2/10.
//  Copyright (c) 2015年 MAC. All rights reserved.
//

#import "HttpRequest.h"

@implementation HttpRequest

- (void)post:(NSString*)url params:(NSString*)params finish:(Result)block failure:(failureResult)failureBlock
{
    self.block = block;
    self.block2=failureBlock;
    self.mutableData = [NSMutableData data];
    self.request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60] ;
//    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [self.request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//     [self.request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [self.request setHTTPMethod:@"post"];
    [self.request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    self.connect = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
}
- (void)post2:(NSString*)url  params:(NSString*)params finish:(Result)block failure:(failureResult)failureBlock
{
    self.block = block;
    self.block2=failureBlock;
    self.mutableData = [NSMutableData data];
    self.request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60] ;
    //    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    [self.request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [self.request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [self.request setHTTPMethod:@"post"];
    [self.request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    self.connect = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
}
- (void)get:(NSString*)url  params:(NSString*)params finish:(Result)block failure:(failureResult)failureBlock
{
    self.block=block;
    self.block2=failureBlock;
     self.mutableData = [NSMutableData data];
     NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    self.connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
#pragma mark - NSURLConnectionDataDelegate
//- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
//{
//    return NO;
//}
//
//- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
//{
//
//    if ([challenge previousFailureCount] ==0)
//
//    {
//
//        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
//        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
//    }
//
//}
//- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
//{
//    return request;
//
//}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);



//    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
//
//        [[challenge sender]  useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
//
//        [[challenge sender]  continueWithoutCredentialForAuthenticationChallenge: challenge];
//
//        NSLog(@"get the whole response");
//
//    }
    
    SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
    [[challenge sender] useCredential: [NSURLCredential credentialForTrust: serverTrust]
           forAuthenticationChallenge: challenge];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.mutableData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.mutableData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *content = [[NSString alloc] initWithData:self.mutableData encoding:NSUTF8StringEncoding];
    self.block(content);
}

#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    self.block2(error);
}

@end
